from rest_framework import serializers
from wxuser.serializers import UserSerializer
from wxuser.serializers import Users


from warehouse.models import JuqiGood, GoodLevel
from warehouse.serializers import JuqiGoodSerializer


from .models import FuliwuGame, FuliwuGood, FuliwuRecoder,FuliEnroll


class FuliwuSer(serializers.ModelSerializer):
    start_time = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )
    end_time = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )
    
    open_time = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )

    hot = serializers.SerializerMethodField()
    user_num = serializers.SerializerMethodField()

    class Meta:
        model = FuliwuGame
        fields = "__all__"
    
    def get_hot(self,obj):
        hot = 0
        _goods = FuliwuGood.objects.filter(game_uuid=obj.uuid).all()
        for _v in _goods:
            hot += _v.price * _v.count
        return hot
    
    def get_user_num(self,obj):
        user_num = FuliEnroll.objects.filter(game_uuid=obj.uuid).count()
        return user_num



    




 #继承Serializer：处理序列化的，可以进行校验;  (但必须要使用BookInfo模型类中所有的字段）
class Fuliwuserializer(serializers.Serializer):
    '''图书数据序列化器'''
    # 表明该字段仅用于序列化输出，默认False
    name=serializers.CharField(max_length=100)

    cover_url = serializers.CharField(max_length=255)
    start_time = serializers.DateTimeField()
    end_time = serializers.DateTimeField()
    consumption_amount = serializers.IntegerField()
    #explain = serializers.UUIDField(required=False)
    open_time = serializers.DateTimeField()




class FuliwuGoodSer(serializers.ModelSerializer):
    good = serializers.SerializerMethodField()
    class Meta:
        model = FuliwuGood
        fields = "__all__"
    
    def get_good(self, obj):
        _good = JuqiGood.objects.filter(uuid=obj.good_uuid).first()
        return {"name":_good.name,"display_url":_good.display_url,"price":_good.price,"recovery_price":_good.recovery_price}
        

class FuliwuRecoderSer(serializers.ModelSerializer):
    good = serializers.SerializerMethodField()
    user = serializers.SerializerMethodField()
    class Meta:
        model = FuliwuRecoder
        fields = "__all__"
    
    def get_good(self, obj):
        _good = JuqiGood.objects.filter(uuid=obj.good_uuid).first()
        if _good:
            return {"name":_good.name,"display_url":_good.display_url,"price":_good.price,"recovery_price":_good.recovery_price}
        else:
            return{"name":"","display_url":"","price":"","recovery_price":""}
    
    def get_user(self,obj):
        _user = Users.objects.filter(uuid=obj.user_uuid).first()
        if _user:
            return {"nickname":_user.nickname,"head_url":_user.head_url,"id":_user.id}
        else:
            return {"nickname":"","head_url":"","id":""}
        

class FuliwuFuliEnrollSer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    class Meta:
        model = FuliEnroll
        fields = "__all__"
        
    def get_user(self,obj):
        _user = Users.objects.filter(uuid=obj.user_uuid).first()
        return {"nickname":_user.nickname,"head_url":_user.head_url,"id":_user.id}


class XcxFuliwuFuliEnrollSer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )
    class Meta:
        model = FuliEnroll
        fields = ("user","createdAt")
        
    def get_user(self,obj):
        _user = Users.objects.filter(uuid=obj.user_uuid).first()
        return {"nickname":_user.nickname,"head_url":_user.head_url,"id":_user.id}